# -*- coding:utf-8 -*-
import re
import requests
import json

"""
拼多多-搜索商品
参考：https://cloudcrawler.club/pin-xi-xi-web-duan-anti-content-can-shu-ni-xiang-fen-xi.html
https://cloud.tencent.com/developer/user/6023914
https://github.com/lateautumn4lin/Review_Reverse
node encrypt_server.js
脚本由 node express运行
"""

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Host": "yangkeduo.com",
    "Upgrade-Insecure-Requests": "1",
    "Cookie": "api_uid=CiWdeF4UFppNHgBDq0etAg==; ua=Mozilla%2F5.0%20(Windows%20NT%2010.0%3B%20Win64%3B%20x64)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F79.0.3945.88%20Safari%2F537.36; _nano_fp=Xpd8l0X8n0CaX5Eyno_57X~_zyIurXQaPvdzc4xs; webp=1; pdd_user_id=6117717858009; pdd_user_uin=TCG4ZCPS7465IP7HTWVNWXB4IA_GEXDA; PDDAccessToken=OUMK2PXTWW5XV2WQOZTWD3OBQHDEGTX3ASNM7M3SGP5LPPMVHX6Q112d903; rec_list_personal=rec_list_personal_3jpni8; JSESSIONID=A110F3481BE0C6998C09661B405FB842",
    "VerifyAuthToken": 'QoLufOnPfs5HWrq45F5frA',
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
}


def get_pdd_search_lst(search_name: str) -> None:
    with requests.get(
        url=f"http://yangkeduo.com/search_result.html?search_key={search_name}",
        headers=headers
    ) as response:
        data = re.findall(r"window.rawData=([\s\S]*?)</script>", response.text)
        if not data:
            raise Exception("extract json error")
        data = data[0].strip().strip(";")
        json_data = json.loads(data)["store"]["data"]["ssrListData"]
        msg_data = dict(json.loads(json_data["loadSearchResultTracking"]["req_params"]),
                        **{"flip": json_data["flip"]})

    headers["AccessToken"] = "OUMK2PXTWW5XV2WQOZTWD3OBQHDEGTX3ASNM7M3SGP5LPPMVHX6Q112d903"
    url = "http://yangkeduo.com/proxy/api/search"
    params = {
        "pdduid": "6117717858009",
        "source": "index",
        "search_met": "manual",
        "track_data": "refer_page_id,10002_1578379866884_vGLBANjfuq;refer_search_met_pos,0",
        "list_id": msg_data["list_id"],
        "sort": "default",
        "filter": "",
        "q": search_name,
        "page": 2,
        "size": 50,
        "flip": msg_data["flip"],
        "anti_content": requests.get('http://localhost:8000/get_anti_content').json()["anti_result"]
    }
    html = requests.get(url, headers=headers, params=params)
    data_json = json.loads(html.text)
    for d in data_json["items"]:
        dict_data = dict()
        dict_data['goods_name'] = d['goods_name']  # 标题名称
        dict_data['short_name'] = d['short_name']  # 标题名称
        dict_data['mall_name'] = d.get('mall_name')  # 店家名称
        dict_data['sales_tip'] = d['sales_tip']  # 拼单件数
        dict_data['sales'] = d['sales']  # 拼单数量 数字
        dict_data['price'] = d['price'] / 100  # 价格
        dict_data['link_url'] = d['link_url']  # 链接
        dict_data['goods_id'] = d['goods_id']  # goods_id

        tag = ""
        for t in d['tag_list']:
            tag += t['text'] + "|"
        dict_data['tag_list'] = tag[:-1]
        get_detail(d['goods_id'])
        print(str(dict_data))

def get_detail(goods_id):
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "max-age=0",
        "Connection": "keep-alive",
        "Cookie": "api_uid=CiWdeF4UFppNHgBDq0etAg==; ua=Mozilla%2f5.0+(Windows+NT+10.0%3b+Win64%3b+x64)+AppleWebKit%2f537.36+(KHTML%2c+like+Gecko)+Chrome%2f73.0.3683.86+Safari%2f537.36; _nano_fp=Xpd8l0X8n0CaX5Eyno_57X~_zyIurXQaPvdzc4xs; webp=1; pdd_user_id=6117717858009; pdd_user_uin=TCG4ZCPS7465IP7HTWVNWXB4IA_GEXDA; PDDAccessToken=OUMK2PXTWW5XV2WQOZTWD3OBQHDEGTX3ASNM7M3SGP5LPPMVHX6Q112d903; JSESSIONID=AC24C97863D20F50A9869546F5A5E1D8",
        "DNT": "1",
        "Host": "yangkeduo.com",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",

    }
    url = f"http://yangkeduo.com/goods.html?goods_id={goods_id}"
    html = requests.get(url, headers=headers)
    pass


get_pdd_search_lst(search_name="女装")
